package com.qbw.core.okhttp;

import com.http.okhttp.HttpTask;
import com.lib.util.ResUtil;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.manager.GeneralManager;
import com.qbw.app.manager.UserManager;
import com.qbw.app.module.common.activity.EmptyActivity;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.request.ErrorCode;

/**
 * @author qinbaowei
 * @createtime 2017/04/22 11:39
 * @email qbaowei@qq.com
 * @description
 */

public class HttpErrorLogic implements HttpTask.ICommonErrorDeal {

    private static HttpErrorLogic sInst;

    private HttpErrorLogic() {
    }

    public static HttpErrorLogic getInstance() {
        if (sInst == null) {
            synchronized (HttpErrorLogic.class) {
                if (sInst == null) {
                    sInst = new HttpErrorLogic();
                }
            }
        }
        return sInst;
    }

    @Override
    public void onFailed(HttpTask httpTask, int code, String message) {
        if (code == ErrorCode.VT_TOKEN_INVALID) {
            UserManager.getInstance().saveUser(null);
            if (httpTask.getWrCallBack().get() != null) {
                UniversalToastUtil.showToast(ResUtil.getString(R.string.login_invalid));
                EmptyActivity.startActivity(XApplication.getInstance(), code);
            }
        }
        /*if (code == LogicErrorCode.TOKEN_TIMEOUT || code == LogicErrorCode.INVALID_TOKEN) {
            XLog.w("token wrong");
            UniversalToastUtil.showToast(ResUtil.getString(R.string.login_invalid));
            UserManager.getInstance().onUserLogout(UserManager.getInstance().getLoginUser());
            //Go.LoginRegistActivity().from(MApplication.getInstance()).from(1).go();
            Go.EmptyActivity().from(MApplication.getInstance()).flag(1).go();
        } *//*else if (alreadDefinedErrorCode(code)) {
            UniversalToastUtil.showToast(message);
        } *//*else if (code == HttpCode.NO_NET) {
            UniversalToastUtil.showToast(R.string.net_error);
        } else if (code == HttpCode.FAILUE) {
            UniversalToastUtil.showToast(R.string.system_error_tip);
        } else {
            UniversalToastUtil.showToast(message);
        }
        Exception ext = new RuntimeException(createExceptionLog(httpTask, code, message));
        CrashReport.postCatchedException(ext);*/
    }

    /*private boolean alreadDefinedErrorCode(int code) {
        return (code >= 1 && code <= 5) || (code >= 100 && code <= 103) || (code >= 200 && code <= 207) || (code >= 300 && code <= 303);
    }

    private String createExceptionLog(HttpTask httpTask, int code, String message) {
        Map<String, String> log = new HashMap<>();
        log.put("_code", String.valueOf(code));
        log.put("_message", message);
        log.put("_url", httpTask.getRealUrl());
        log.put("_time", DateTimeUtil.formatYYYYMMDDHHMM(System.currentTimeMillis()));
        log.put("_appVersion", BuildConfig.VERSION_NAME);
        log.put("_device", DeviceUtil.getUA());
        log.putAll(httpTask.getParams());
        return new Gson().toJson(log);
    }*/
}
